/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package editor;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import map.MemMap;

import db.DataSource;
import figures.Figure;
import figures.basic.PoliLine;
import figures.cartography.Avenue;
import figures.cartography.SpeedWay;
import figures.cartography.Square;
import figures.cartography.Street;
import figures.cartography.Train;
import geometry.Point;

public class aaa {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BufferedReader r = new BufferedReader(new FileReader("bsas.txt"));
			String l = r.readLine();
			String tipo = null;
			String tipo_b = null;
			float x;
			float y;
			String nombre =null;
			while(l!=null){
				String[] ss = l.split("=");
				if(ss.length > 1){
					if(ss[0].equals("DESCRIPTION"))
						tipo = ss[1];
					if(ss[0].equals("NAME"))
						nombre = ss[1];
					if(ss[0].equals("NOMBRE"))
						nombre = ss[1];
					if(ss[0].equals("TIPO"))
						tipo_b = ss[1];
				}else{
					ss = l.split(",");
					if(ss.length > 1){
						x= Float.parseFloat(ss[0]);
						y= Float.parseFloat(ss[1]);
						if(nombre!= null)
							addCoordinate(x, y);
					}else{
						addObject(nombre, tipo, tipo_b);
						nombre = tipo = tipo_b = null;
					}
				}
				
				l = r.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//while(merge())
		//	System.out.print(".");
		//while(merge2());
		merge3();
		
		
		for(Figure f:list)
			map.appendFigure(f);
		
		ds = new DataSource();
		ds.setMapName("bsas");
		ds.setVersion("1.2");
		ds.write(map);
	}
	
	
	private static void merge3() {
		Map<String, List<Figure>> m = new HashMap<String, List<Figure>>();
		for(Figure f2:list){
			String s2 = new String(f2.getName());
			List<Figure> l = null;
			if(!m.containsKey(s2)){
				l = new ArrayList<Figure>();
				m.put(s2, l);
			}else
				l = m.get(s2);
			l.add(f2);
		}
		for(List<Figure> l:m.values()){
			while(merge(l))
				System.out.print(".");
			System.out.println(".");
		}
		list.clear();
		for(List<Figure> l:m.values()){
			list.addAll(l);
		}
		System.out.println("quedaron "+list.size());
	}


	private static boolean merge2() {
		Figure f1 = null;
		Set<Figure> todelete = new HashSet<Figure>();
		for(Figure f2:list){
			PoliLine p2=(PoliLine)f2;
			int[] x2 = p2.getx();
			int[] y2 = p2.gety();
			String s2 = new String(p2.getName());
			if(f1!=null){
				PoliLine p1=(PoliLine)f1;
				int[] x1 = p1.getx();
				int[] y1 = p1.gety();
				String s1 = new String(p1.getName());
				if(s1.equals(s2) && (x1[x1.length-1]==x2[0] && y1[y1.length-1]==y2[0])){
					int[] nx = new int[x1.length+x2.length-1];
					int[] ny = new int[y1.length+y2.length-1];
					System.arraycopy(x1, 0, nx, 0, x1.length);
					System.arraycopy(y1, 0, ny, 0, y1.length);
					System.arraycopy(x2, 1, nx, x1.length, x2.length-1);
					System.arraycopy(y2, 1, ny, y1.length, y2.length-1);
					p1.setxy(nx, ny);
					todelete.add(f1);
				}else
					f1 = f2;
			}else
				f1 = f2;
			
		}
		System.out.println("merged "+todelete.size()+" remain "+list.size());
		list.removeAll(todelete);
		return todelete.size()!=0;
	}

	/**
	 * @param todelete
	 */
	private static boolean merge(List<Figure> lista) {
		Set<Figure> todelete = new HashSet<Figure>();
		for(Figure f1:lista){
			if(todelete.contains(f1))
				continue;
			PoliLine p1=(PoliLine)f1;
			int[] x1 = p1.getx();
			int[] y1 = p1.gety();
			String s1 = new String(p1.getName());
			for(Figure f2:lista){
				if(f1==f2 || todelete.contains(f2))
					continue;
				PoliLine p2=(PoliLine)f2;
				int[] x2 = p2.getx();
				int[] y2 = p2.gety();
				String s2 = new String(p2.getName());
				if(s1.equals(s2))
					if(x1[x1.length-1]==x2[0] && y1[y1.length-1]==y2[0]){
						int[] nx = new int[x1.length+x2.length-1];
						int[] ny = new int[y1.length+y2.length-1];
						System.arraycopy(x1, 0, nx, 0, x1.length);
						System.arraycopy(y1, 0, ny, 0, y1.length);
						System.arraycopy(x2, 1, nx, x1.length, x2.length-1);
						System.arraycopy(y2, 1, ny, y1.length, y2.length-1);
						p1.setxy(nx, ny);
						todelete.add(f2);
						break;
					}
			}
		}
		lista.removeAll(todelete);
		return todelete.size()!=0;
	}
	static MemMap map = new MemMap(2000);
	static List<Figure> list = new ArrayList<Figure>();
	static DataSource ds = null;
	static Point[] points = new Point[0];
	

	private static void addObject(String nombre, String tipo, String tipo_b) {
		if(points.length==0)
			return;
		Figure f=null;
		if(tipo.equals("Residential Road")){
			if(tipo_b.equals("1"))
				f = new Street(points, nombre);
			if(tipo_b.equals("2"))
				f = new Avenue(points, nombre);
			if(tipo_b.equals("3"))
				f = new SpeedWay(points, nombre);
		}
		if(tipo.equals("Railroad"))
			f = new Train(points, nombre);
		if(tipo.equals("City Park"))
			f = new Square(points, nombre);
		//"Unknown Area Type"
		//"River, Unknown Length"
		//"Major River"
		if(f!=null)
			list.add(f);
			
		points = new Point[0];
	}

	private static void addCoordinate(float x, float y) {
		Point[] tmp = new Point[points.length+1];
		int lx = (((int)(x*100000f)) + 5853241)/5;
		int ly = (-(((int)(y*100000f)) + 3470523))/5;
		tmp[points.length] = new Point(lx, ly);
		System.arraycopy(points, 0, tmp, 0, points.length);
		points = tmp;
	}

}
